﻿#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <hadoop/SerialUtils.hh>
#include <gtest/gtest.h>
#include <glog/logging.h>

#include <QtCore/QTime>

class SerializeTest : public ::testing::Test {
protected:
	virtual void SetUp() {
		QTime time = QTime::currentTime();
		qsrand((uint)time.msec());
	}
	virtual void TearDown() {}
	
};
TEST(SerializeTest, serializeInt) { 
	for(int i=0;i!=1000;++i){
		int value=qrand();
		HadoopUtils::StringOutStream out;
		HadoopUtils::serializeInt(value,out);
		HadoopUtils::StringInStream is(out.str());
		auto value2=HadoopUtils::deserializeInt(is);
		EXPECT_EQ(value, value2);
	}
}

TEST(SerializeTest, serializeFloat) { 

	for(int i=0;i!=1000;++i){
		int value=qrand();
		value &= (1<<24)-1;
		float v=(float)value/(1<<24);
		HadoopUtils::StringOutStream out;
		HadoopUtils::serializeFloat(v,out);
		HadoopUtils::StringInStream is(out.str());
		auto v2=HadoopUtils::deserializeFloat(is);
		EXPECT_EQ(v, v2);
	}

}

class MyEnvironment :public ::testing::Environment {
public:
	virtual ~MyEnvironment() {}
	// Override this to define how to set up the environment.
	virtual void SetUp() {
		google::InitGoogleLogging("myunitest");
	}
	// Override this to define how to tear down the environment.
	virtual void TearDown() {}
};

int main(int argc, char **argv) {
	::testing::InitGoogleTest(&argc, argv);
	::testing::AddGlobalTestEnvironment(new MyEnvironment());
	return RUN_ALL_TESTS();
}